package cn.com.surker.production.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.production.car.CarWorkStatusEnum;
import cn.com.surker.production.dao.TranCarDao;
import cn.com.surker.production.entity.TranCar;
import cn.com.surker.production.entity.vo.TranCarVo;
import cn.com.surker.production.service.TranCarService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 生产车辆表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-04
 */
public abstract class TranCarServiceBaseImpl extends ServiceImpl<TranCarDao, TranCar> implements TranCarService {

	@Resource
	protected TranCarDao tranCarDao;

	@Override
	public List<TranCarVo> findOrgList(Long orgId, List<Integer> carStatusList, String sortBy) {
		if (orgId == null) {
			return null;
		}
		List<TranCarVo> tranCars = tranCarDao.findOrgList(orgId, carStatusList, sortBy);
		return tranCars;
	}

	/**
	 * 查询运输车辆信息
	 * @param contractId
	 * @return
	 */
	public List<TranCar> findAll(Long contractId) {
		QueryWrapper<TranCar> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		if (!StringUtils.isNull(contractId))
			qw.notInSql("id", "select tran_car_id from production_tran_con_car " +
					"where status = 0 and pro_con_id = " + contractId);
		qw.orderByDesc("id");
		List<TranCar> list = tranCarDao.selectList(qw);
		return list;
	}

	@Override
	public TranCar findByCarNo(String carNo) {
		if (StringUtils.isBlank(carNo)) {
			return null;
		}
		QueryWrapper<TranCar> qw = new QueryWrapper<>();
		qw.eq("car_no", carNo);
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.last("limit 1");
		TranCar tranCar = tranCarDao.selectOne(qw);
		return tranCar;
	}

	@Override
	public IPage<TranCarVo> selectPage(String code, String carNo, Integer pageIndex, Integer limit) {
		QueryWrapper<TranCarVo> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		if (StringUtils.isNotBlank(code)) {
			qw.like("code", code);
		}
		if (StringUtils.isNotBlank(carNo)) {
			qw.like("car_no", carNo);
		}
		PageHelper.startPage(pageIndex, limit);
		IPage<TranCarVo> iPage = tranCarDao.selectFullPage(new Page<>((pageIndex + limit) / limit, limit), qw);
		/**分页插件**/
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				iPage.setTotal(pageInfo.getTotal());
		}
		return iPage;
	}

	@Override
	public int updateStatus(Long orgId, Long carId, Integer carStatus, boolean requeue, Long invoiceId) {
		/*UpdateWrapper<TranCar> uw = new UpdateWrapper<>();
		uw.eq("id", carId);
		uw.set("car_status", carStatus);
		int update = tranCarDao.update(null, uw);*/
		TranCar tranCar = new TranCar();
		tranCar.setId(carId);
		tranCar.setCarStatus(carStatus);
		if (CarWorkStatusEnum.WAIT.getCode().equals(carStatus) && requeue) {
			//	上号更新签到时间
			tranCar.setSignTime(new Date());
			//tranCar.setSignType()
		}
		if (invoiceId != null) {
			tranCar.setInvoiceId(invoiceId);
		}
		int i = tranCarDao.updateById(tranCar);
		return i;
	}

	@Override
	public String queryCarCode(Long orgId, Long carId, String taskTime) {
		if (carId == null) {
			return null;
		}
		String carCode = tranCarDao.queryCarCode(orgId, carId, taskTime);
		return carCode;
	}

	@Override
	public Long queryCarId(Long orgId, String code, String taskTime) {
		if (StringUtils.isBlank(code)) {
			return null;
		}
		Long carId = tranCarDao.queryCarId(orgId, code, taskTime);
		return carId;
	}

	@Override
	public List<TranCarVo> findQuotaList(Long orgId) {
		List<TranCarVo> quotaList = tranCarDao.findQuotaList(orgId);
		return quotaList;
	}

}